#!/usr/bin/env node

/**
 * Test script for execute-parallel integration
 * This script simulates calling the execute-parallel tool
 */

import { executeParallelHandler } from './dist/tools/execute-parallel.js';

async function testExecuteParallel() {
  console.log('🧪 Testing execute-parallel integration...\n');

  const context = {
    projectPath: '/Users/mac/work/github/merge/spec-workflow-mcp-pro'
  };

  try {
    // Test 1: Dry run to verify basic functionality
    console.log('📋 Test 1: Dry run with external agent integration');
    const dryRunResult = await executeParallelHandler({
      specName: 'test-integration',
      dryRun: true,
      maxParallel: 2
    }, context);

    console.log('✅ Dry run result:');
    console.log('Success:', dryRunResult.success);
    console.log('Message length:', dryRunResult.message?.length || 0, 'characters');
    console.log('\n' + '='.repeat(80) + '\n');

    // Test 2: Check if external agents are discovered
    console.log('📋 Test 2: Agent discovery test');

    // Import and test ExternalAgentSystem directly
    const { ExternalAgentSystem } = await import('./dist/parallel/agents/external-agent-system.js');

    const agentSystem = new ExternalAgentSystem();
    await agentSystem.initialize();

    const availableAgents = agentSystem.getAvailableAgentNames();
    console.log('✅ Discovered agents:', availableAgents.length);
    console.log('Agent names:', availableAgents.slice(0, 5).join(', '), availableAgents.length > 5 ? '...' : '');

    const stats = agentSystem.getStats();
    console.log('Agent sources:', stats);
    console.log('\n' + '='.repeat(80) + '\n');

    // Test 3: Task type identification
    console.log('📋 Test 3: Task type identification');

    const { TaskTypeIdentifier } = await import('./dist/parallel/agents/task-type-identifier.js');
    const identifier = new TaskTypeIdentifier();

    const sampleTask = {
      id: 'test-backend-task',
      description: 'Create a simple backend API endpoint for user authentication',
      requirements: 'Implement REST API with authentication logic',
      completed: false,
      parallelSafe: true,
      dependencies: []
    };

    const analysis = identifier.identifyTaskType(sampleTask);
    console.log('✅ Task analysis result:');
    console.log('Task types:', analysis.taskTypes);
    console.log('Confidence:', analysis.confidence.toFixed(2));
    console.log('Keywords:', analysis.keywords);
    console.log('\n' + '='.repeat(80) + '\n');

    // Test 4: Agent selection
    console.log('📋 Test 4: Intelligent agent selection');

    const { IntelligentAgentSelector } = await import('./dist/parallel/agents/intelligent-agent-selector.js');
    const selector = new IntelligentAgentSelector();

    const selection = selector.selectAgentsForTask(sampleTask, analysis.taskTypes, availableAgents, 3);
    console.log('✅ Agent selection result:');
    console.log('Selected agents:', selection.selectedAgents);
    console.log('Alternative agents:', selection.alternativeAgents);
    console.log('Confidence:', selection.confidenceScore.toFixed(2));
    console.log('\n' + '='.repeat(80) + '\n');

    console.log('🎉 All tests completed successfully!');
    console.log('\n📊 Integration Summary:');
    console.log(`✅ External Agent System: ${stats.total} agents from ${stats.ccpm} CCPM + ${stats.superclaude} SuperClaude`);
    console.log(`✅ Task Type Identification: ${analysis.taskTypes.length} types detected`);
    console.log(`✅ Agent Selection: ${selection.selectedAgents.length} agents selected`);
    console.log(`✅ Execute Parallel: Dry run successful`);

  } catch (error) {
    console.error('❌ Test failed:', error.message);
    console.error('Stack trace:', error.stack);
  }
}

// Run the test
testExecuteParallel().catch(console.error);